'use strict'

const Controller = require('egg').Controller

class HomeController extends Controller {
  //车站数据
  async stationList() {
    const { ctx } = this
    const list = await this.app.mysql.select('station')
    ctx.body = {
      code: 200,
      msg: '车站数据请求成功',
      list,
    }
  }
  //删除
  async stationDel() {
    const { ctx } = this
    const { id } = ctx.query
    const res = await this.app.mysql.delete('station', {
      id,
    })
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('station')
      ctx.body = {
        code: 200,
        msg: '删除成功',
        list,
      }
    } else {
      ctx.body = {
        code: 404,
        msg: '删除失败',
      }
    }
  }

  //编辑
  async stationEdit() {
    const { ctx } = this
    const { id, station } = ctx.request.body
    const row = {
      id,
      station,
    }
    const res = await this.app.mysql.update('station', row)
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('station')
      ctx.body = {
        code: 200,
        msg: '修改成功',
        list,
      }
    } else {
      ctx.body = {
        code: 404,
        msg: '修改失败',
      }
    }
  }

  //添加
  async stationAdd() {
    const { ctx } = this
    const { station, falg } = ctx.request.body
    const res = await this.app.mysql.insert('station', {
      station,
      falg: 'false',
    })
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('station')
      ctx.body = {
        code: 200,
        msg: '添加成功',
        list,
      }
    } else {
      ctx.body = {
        code: 404,
        msg: '添加失败',
      }
    }
  }
}

module.exports = HomeController
